<?php
  require_once(ROOT_PATH.'ty/database_conf.php');
  require_once(ROOT_PATH.'ty/DataBaseDao.php');
  class ExchangeRateService extends dataBaseDao{
      
      public function ExchangeRateService(){
          
      }
      
      public function getLastExchangeRateList(){
          $currentTime = time();
          $fiveMinBefore = $currentTime-600;
          
          $list = $this->getExchangeRateListByTime($fiveMinBefore);
          
          if(count($list) <= 0){
              $rateList = $this->collectFx168Rate();
              $this->saveRateList($rateList, $currentTime);
              $list = $this->getExchangeRateListByTime($currentTime);
          }
          
          //$this->getDb()->close();
          return $this->buildExchangeRateHtml($list);
      }
      
      public function getStockRateListHtml(){
          $list = $this->getStockRateList(time()-600);
          
          if(count($list) <= 0){
              $stockList = $this->getStockListFromFx168();
              $current = time();
              $this->saveRateList($stockList, $current);
              $list = $this->getStockRateList(time()-600);
          }
          //$this->getDb()->close();
          return $this->buildExchangeRateHtml($list);
      }
      
      public function buildExchangeRateHtml($list){
          if(count($list) <= 0) return '';
          $html = '<table width="100%" border="0" cellspacing="0" >';
          foreach($list as $k=>$v){
              $html .= '<tr>';
              foreach($v as $rk=>$rv){
                  if($rk =='rate_id' || $rk == 'load_time' || 'rate_type' == $rk)continue;
                  $html .= '<td>'.$rv.'</td>';
              }
              $html .= '</tr>';
          }
          $html .= '</table>';
          
          $html = str_replace('"', '\'', $html);
          return $html;
      }
      
      function getExchangeRateListByTime($time){
          $sql = "select * from exchange_rate_info where load_time >= $time and rate_type='exchange' order by rate_id ";
          $list = $this->getDb()->fetchRows($sql);
          return $list;
      }
      
      function getStockRateList($time){
          $sql = "select * from exchange_rate_info where load_time >= $time and rate_type='stock' order by rate_id ";
          $list = $this->getDb()->fetchRows($sql);
          return $list;
      }
      
      function swapDoubleToSingalQuote($text){
         $text = str_replace('"', '\'', $text);
         return $text;
      }
      
      function collectFx168Rate(){
          $html = file_get_html('http://www.fx168.com/');
          
          $tt = $html->find('div[id=I_Currency]',0);
          $t_table = $tt->find('table',0);
          $str = '<table width="100%" border="0" cellspacing="0" >';
          $r = array();
          $rowIndex = 0;
          foreach($t_table->find('tr') as $k=>$v){
              $i = 0;
              $row = array();
              $rowIndex++;
              $str .= '<tr>';
              
             foreach($v->find('td') as $tdk=>$tdv){
                  if($tdv->find('a',0)){
                       $row[$i] = iconv('gb2312', 'utf-8', $this->swapDoubleToSingalQuote($tdv->find('a',0)->innertext));
                       $str .= '<td>'.iconv('gb2312', 'utf-8', $this->swapDoubleToSingalQuote($tdv->find('a',0)->innertext)).'</td>';
                 }else{
                     $row[$i] = iconv('gb2312', 'utf-8', $this->swapDoubleToSingalQuote($tdv->innertext));
                     $str .= '<td>'.iconv('gb2312', 'utf-8', $this->swapDoubleToSingalQuote($tdv->innertext)).'</td>';
                 }
                 $i++;
             }
             $row[$i] = 'exchange';
             array_push($r, $row);
             $str .= '</tr>';
             
         }
         
         $str .= '</table>';
         return $r;
      }
    
      function getStockListFromFx168(){
          $html = file_get_html('http://www.fx168.com/');
          $tt = $html->find('div[id=I_stock]',0);
          $t_table = $tt->find('table',0);
          
          $list = array();
          foreach($t_table->find('tr') as $k=>$row){
              $r = array();
              $rowIndex = 0;
              foreach($row->find('td') as $tdK=>$tdV){
                  if($tdV->find('a',0)){
                      $r[$rowIndex] = $tdV->find('a',0)->innertext;
                  }else{
                      $r[$rowIndex] = $tdV->innertext;
                  }
                  
                  $r[$rowIndex] = iconv('gb2312', 'utf-8', $this->swapDoubleToSingalQuote($r[$rowIndex]));
                  $rowIndex ++;
              }
            if($rowIndex <= 1){
                unset($r);
                continue;
            }  
            $r[$rowIndex] = 'stock';
            array_push($list, $r);
          }
          
          return $list;
      }
    
      private function saveRateList($rateList, $time){
          foreach($rateList as $k=>$v){
              $this->insertExchangeRate($v, $time);
          }
      }
      
     private function insertExchangeRate($info, $time){
          $sql = "insert into exchange_rate_info(rate_title,current_val,float_val,rate_type,load_time) ";
          $sql .= "values('".addslashes($info[0])."','".addslashes($info[1])."','".addslashes($info[2])."','".$info[3]."',".$time.") ";
          $this->getDb()->query($sql);
      }
      
  }
  
  
?>
